package com.qdlc.p2p.dal.model;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.exception.RedEnvelopeException;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.WineCoupons;

/**
 * 酒券方案 Model 
 * @author ygw
 * @version 1.1.0.0
 * @date 2016年5月6日 下午2:23:37
 */
public class WineCouponsModel extends WineCoupons {
	
	private static final Logger logger = Logger.getLogger(WineCouponsModel.class);
	/**
	 * 序列
	 */
	private static final long serialVersionUID = 1L;

	/********** 常量 start *************/

	/**
	 * 状态：启用
	 */
	public static final byte WINE_COUPONS_STATUS_ENABLE = 1;

	/**
	 * 状态：不启用
	 */
	public static final byte WINE_COUPONS_STATUS_DISABLE = 0;

	/**
	 * 酒券类型：现金酒券
	 */
	public static final byte WINE_COUPONS_TYPE_CASH = 1;

	/**
	 * 发放方式：固定金额
	 */
	public static final byte WINE_COUPONS_PAY_FIXED_AMOUNT = 1;

	/**
	 * 是否删除：删除
	 */
	public static final byte WINE_COUPONS_IS_DELETE = 1;

	/********** 常量 end *************/

	/**
	 * 当前页数
	 */
	private int page;

	/**
	 * 每页总数
	 */
	private int rows;
	
	/**
	 * 发放类型描述
	 */
	private String typeStr;
	
	/**
	 * 发放方式描述
	 */
	private String payTypeStr;

	/**
	 * 快速查询条件
	 */
	private String searchName;
	
	/**
	 * 实例化RedEnvelopeModel
	 * 
	 * @param redEnvelope
	 * @return RedEnvelopeModel
	 */
	public static WineCouponsModel instance(WineCoupons wineCoupons) {
		WineCouponsModel model = new WineCouponsModel();
		BeanUtils.copyProperties(wineCoupons, model);
		return model;
	}
	
	/**
	 * 根据Model返回实体原型
	 * 
	 * @return RedEnvelope
	 */
	public WineCoupons prototype() {
		WineCoupons wineCoupons = new WineCoupons();
		BeanUtils.copyProperties(this, wineCoupons);
		return wineCoupons;
	}
	
	/**
	 * 检测发放酒券常规内容：1、是否开启；2、是否已发放完毕；3、是否已被删除
	 */
	@SuppressWarnings("static-access")
	public boolean checkUseModel() {
		//是否开启
		if (this.WINE_COUPONS_STATUS_DISABLE == this.getStatus()) {
			logger.info("酒券方案:"+ getId() +", "+MessageUtil.getMessage("MF101001006"));
			return false;
		}
		//是否已发放完毕
		if (this.getTotal() != 0 && (this.getTotal() < this.getIssueNum() + 1)) {
			logger.info("酒券方案:"+this.getId()+", "+MessageUtil.getMessage("MF101001007"));
			return false;
		}
		//是否已被删除
		if (this.WINE_COUPONS_IS_DELETE == this.getIsDelete()) {
			logger.info("酒券方案:"+this.getId()+", "+MessageUtil.getMessage("MF101001008"));
			return false;
		}
		return true;
	}
	
	/**
	 * 添加酒券方案检测
	 */
	public void checkAddModel() {
		//酒券名称是否为空
		if (StringUtil.isBlank(this.getName())) {
			throw new RedEnvelopeException(MessageUtil.getMessage("MF101001009"));
		}
		//发放类型是否正确
		if (this.getType() != WINE_COUPONS_TYPE_CASH) {
			throw new RedEnvelopeException(MessageUtil.getMessage("MF101001011"));
		}
		//发放方式是否正确
		if (this.getPayType() != WINE_COUPONS_PAY_FIXED_AMOUNT) {
			throw new RedEnvelopeException(MessageUtil.getMessage("MF101001010"));
		}
		//发放金额是否于发放方式对应
		if (this.getPayType() == WINE_COUPONS_PAY_FIXED_AMOUNT && this.getAmount() <= 0) {
			throw new RedEnvelopeException(MessageUtil.getMessage("MF101001012"));
		}
	}
	
	/**
	 * 获取当前页数
	 * 
	 * @return 当前页数
	 */
	public int getPage() {
		return page;
	}
	
	/**
	 * 设置当前页数
	 * 
	 * @param page
	 *            当前页数
	 */
	public void setPage(int page) {
		this.page = page;
	}

	/**
	 * 获取每页总数
	 * 
	 * @return 每页总数
	 */
	public int getRows() {
		return rows;
	}

	/**
	 * 设置每页总数
	 * 
	 * @param rows
	 *            每页总数
	 */
	public void setRows(int rows) {
		this.rows = rows;
	}

	/**
	 * 获取发放类型
	 * 
	 * @return
	 */
	public String getTypeStr() {
		switch (getType()) {
		case WINE_COUPONS_TYPE_CASH:
			typeStr = "现金酒券";
			break;
		default:
			break;
		}
		return typeStr;
	}

	/**
	 * 设置发放类型
	 * 
	 * @param typeStr
	 */
	public void setTypeStr(String typeStr) {
		this.typeStr = typeStr;
	}

	/**
	 * 获取发放方式
	 * 
	 * @return
	 */
	public String getPayTypeStr() {
		switch (getPayType()) {
		case WINE_COUPONS_PAY_FIXED_AMOUNT:
			payTypeStr = "固定金额";
			break;
		default:
			break;
		}
		return payTypeStr;
	}

	/**
	 * 设置发放方式
	 * 
	 * @param payTypeStr
	 */
	public void setPayTypeStr(String payTypeStr) {
		this.payTypeStr = payTypeStr;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}
	
	
}
